<script setup>
import { ref } from 'vue'
import { ElMessage } from "element-plus";

import { userRegisterService,userLoginService } from '@/api/user.js'


//定义数据模型
const userData = ref({
    username: '',
    pwd: '',
    rePassword: ''
})

//注册函数
const register = async () => {
    //registerData是一个响应式对象，如果要获取值，需要.value
    let result = await userRegisterService(userData.value);
    // if (result.code === 0) {
    //     //成功了
    //     alert(result.message ? result.message : '注册成功')
    // } else {
    //     alert('注册失败')
    // }
    // alert(result.message ? result.message : '注册成功')
    ElMessage.success(result.message ? result.message : '注册成功')

}

//登录函数
import {useRouter} from 'vue-router'
const router = useRouter()
const login = async () =>{
    //调用接口，完成登录
    let result = await userLoginService(userData.value);


    let token = result.data
    console.log(token);
    localStorage.setItem('token',token)
    if (token){
        ElMessage.success(result.message ? result.message : '登录成功')
        //跳转到首页 路由完成跳转
        await router.push({path: '/'})
    }else {
        ElMessage.error('登录失败')
    }



}


</script>

<template>
    <el-form :model="userData" class="login-form">
        <h2 class="login-title">学生后台管理系统</h2>

        <el-form-item label="账号" prop="username">
            <el-input
                size="large"
                placeholder="请输入账号"
                v-model="userData.username"
                prefix-icon="User"
            ></el-input>
        </el-form-item>

        <el-form-item label="密码" prop="pwd">
            <el-input
                size="large"
                placeholder="请输入密码"
                v-model="userData.pwd"
                show-password
                prefix-icon="Lock"
            ></el-input>
        </el-form-item>

        <el-form-item>
            <el-button
                type="primary"
                class="login-button"
                @click="login"
            >登录</el-button>
        </el-form-item>
    </el-form>
</template>

<style scoped>
.login-form {
    width: 400px;
    padding: 30px;
    border-radius: 8px;
    box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
    background-color: white;
    margin: auto;
}

.login-title {
    font-size: 24px;
    font-weight: 600;
    text-align: center;
    margin-bottom: 30px;
    color: #303133;
}

.login-button {
    width: 100%;
    padding: 12px 0;
    font-size: 16px;
    border-radius: 4px;
}

:deep(.el-form-item) {
    margin-bottom: 25px;
}

:deep(.el-input__wrapper) {
    border-radius: 4px;
}

:deep(.el-form-item__label) {
    font-weight: 500;
    color: #606266;
}
</style>